<?php

/**
 * This is the model class for table "{{district}}".
 *
 * The followings are the available columns in table '{{district}}':
 * @property integer $id
 * @property integer $province_id
 * @property string $district_name
 * @property string $name_jp
 * @property string $create_date
 * @property integer $created_by
 * @property string $update_date
 * @property integer $updated_by
 *
 * The followings are the available model relations:
 * @property Province $province
 * @property Profile[] $profiles
 */
class District extends CActiveRecord
{
    public $keyword;
    /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return District the static model class
        */
    public static function model($className=__CLASS__)
    {
            return parent::model($className);
    }

    /**
        * @return string the associated database table name
        */
    public function tableName()
    {
            return '{{district}}';
    }

    /**
        * @return array validation rules for model attributes.
        */
    public function rules()
    {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                    array('province_id, district_name', 'required', 'message' => t ( 'MSG_REQUIRED', 'messages' )),
                    array('province_id', 'numerical', 'integerOnly'=>true, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
                    array('district_name, name_jp', 'length', 'max'=>255, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),                    
                    // The following rule is used by search().
                    // Please remove those attributes that should not be searched.
                    array('id, province_id, district_name, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
                    array('keyword', 'length', 'max' => 100, 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages')),
            );
    }

    /**
        * @return array relational rules.
        */
    public function relations()
    {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                    'province' => array(self::BELONGS_TO, 'Province', 'province_id'),
                    'profiles' => array(self::HAS_MANY, 'Profile', 'district_id'),
            );
    }

    /**
        * @return array customized attribute labels (name=>label)
        */
    public function attributeLabels()
    {
            return array(
                    'id' => t('LBL_ID', 'district'),
                    'province_id' => t('LBL_PROVINCE_NAME', 'district'),
                    'district_name' => t('LBL_DISTRICT_NAME', 'district'),
            		'name_jp' => t('LBL_NAME_JP', 'district'),
                    'create_date' => t('LBL_CREATE_DATE', 'district'),
                    'created_by' => t('LBL_CREATED_BY', 'district'),
                    'update_date' => t('LBL_UPDATE_DATE', 'district'),
                    'updated_by' => t('LBL_UPDATED_BY', 'district'),
            );
    }

    /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);				
        $criteria->addSearchCondition('district_name', $this->keyword, true); 
        $criteria->compare('province_id',$this->province_id);

        $sort = new CSort;
        $sort->defaultOrder = array('district_name' => CSort::SORT_DESC);
        $sort->attributes = array(
            '*'                
        );

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => LGHelpers::getPageSize($this->count()),
            ),
            'sort' => $sort
        ));
    }
    
    public function beforeSave() {
        if (parent::beforeSave()) {

            $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);

            //Get user is logging
            $logedInUser = User::model()->findByPk(Yii::app()->user->id);	   

            if ($this->isNewRecord) {
		$this->create_date = $current_date;
		$this->created_by = $logedInUser->username;
	    }
            
            $this->update_date = $current_date;

            if (isset($logedInUser)) {
                $this->updated_by = $logedInUser->username;
            }	    

            $this->setIsNewRecord(false);
            return true;
        } else {
            return false;
        }
    }
}